<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitNewLotteryRecord extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 抽奖签号
        Schema::create('lottery_tickets', function (Blueprint $table) {

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->char('number', 20)->nullable()->comment('编号');

            $table->integer('activity_id')->index()->comment('活动id');
            $table->integer('user_id')->index()->comment('用户id');
            $table->integer('activity_record_id')->index()->comment('活动参与记录id');

            $table->integer('session')->index()->comment('第几期');

            $table->integer('order_id')->nullable()->comment('订单id');
            $table->integer('package_sku_id')->nullable()->comment('奖品sku id');
            $table->integer('sku_id')->nullable()->comment('中奖的奖品id');
            // $table->boolean('is_picked')->default(0)->index()->comment('0为未领取、1为已领取'); // 是否中奖

            $table->tinyInteger('source_type')->default(1)->comment('1参与获得、2助力获得');
            $table->integer('invitee_id')->nullable()->index()->comment('助力者id');
            $table->integer('invite_record_id')->nullable()->index()->comment('助力记录');

            $table->boolean('status')->default(0)->index()->comment('0等待开奖 1已中奖 2未中奖'); // 是否中奖

            $table->softDeletes();
            $table->timestamps();
        });

        // 参与记录
        Schema::create('lottery_records', function (Blueprint $table) {
            $table->comment = '活动参与记录表';
            
            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->integer('user_id')->index()->comment('用户id'); // 用户id
            $table->integer('activity_id')->index()->comment('用户id'); // 用户id
            $table->integer('order_id')->index()->nullable()->comment('订单id'); // 订单id
            $table->char('join_code', 30)->nullable()->comment('参与码'); // 参与码

            $table->integer('session')->index()->comment('第几期');

            // $table->integer('power')->default(10)->index()->comment('能量值 (决定中奖概率)'); // 能量值 (决定中奖概率)

            $table->boolean('is_lucky_user')->default(0)->index()->comment('是否中奖'); // 是否中奖

            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('1正常'); // 0正常

            $table->softDeletes();
            $table->timestamps();
        });

        // 助力记录
        Schema::create('lottery_invite_records', function (Blueprint $table) {
            $table->comment = '活动助力记录表';
            
            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->integer('user_id')->index()->comment('用户id'); // 用户id
            $table->integer('activity_id')->index()->comment('用户id'); // 用户id
            $table->integer('activity_record_id')->nullable()->index()->comment('用户参与记录id'); // 用户id

            $table->integer('session')->index()->comment('第几期');

            $table->integer('order_id')->index()->nullable()->comment('订单id'); // 订单id

            $table->integer('invitee_id')->index()->comment('受邀者id'); // 用户id
            $table->boolean('is_new_user')->index()->default(0)->comment('邀请的是否是新注册用户'); // 用户id

            $table->integer('ticket_total')->nullable()->comment('受邀请获得的抽奖号'); // 订单id

            $table->json('options')->nullable()->comment('options信息'); // 用户id

            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('1正常'); // 0正常

            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('lottery_ticket');
        Schema::dropIfExists('lottery_records');
        Schema::dropIfExists('lottery_invite_records');
    }
}
